<template>
  <div class="employee-container">
    <div class="building-container">
      <el-form ref="form" label-width="80px">
        <el-row style="border-bottom: 1.5px solid #f4f6f8;">
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <el-form-item label="员工姓名">
                <el-input v-model="name" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="14" style="margin-left: -60px !important;">
            <div class="grid-content bg-purple-light"> <el-form-item>
              <el-button type="primary" @click="List">查询</el-button>
            </el-form-item></div>
          </el-col>
        </el-row>
      </el-form>
      <div class="div">
        <el-row>

          <el-button type="primary" @click="add('add')">添加员工</el-button>

        </el-row>
      </div>
      <div>
        <el-row>
          <el-table style="width: 100%" :data="tableData">
            <el-table-column type="index" label="序号" />
            <el-table-column prop="name" label="员工姓名" width="180" />
            <el-table-column prop="userName" label="登录账号" />
            <el-table-column prop="phonenumber" label="联系方式" width="120" />
            <el-table-column prop="roleName" label="角色" width="120" />
            <el-table-column prop="status" label="状态" />
            <el-table-column prop="createTime" label="添加时间" width="150" />
            <el-table-column label="操作">
              <template #default="{row}">
                <el-link type="primary" @click="edit('edit',row.id)"> 编辑</el-link>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <el-link type="primary" @click="del(row.id)">删除</el-link>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <el-link type="primary" @click="reset(row.id)">重置密码</el-link>
              </template>

            </el-table-column>

          </el-table>
        </el-row>
        <!-- 分页 -->
        <div class="block">
          <el-pagination
            :page-sizes="[10,20,30,40,50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="+total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
      <el-dialog
        :title="title"
        :visible="dialogVisible"
        width="480px"
        @close="closeDialog"
      >
        <!-- 表单接口 -->
        <div class="form-container">
          <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="80px">
            <el-form-item label="员工姓名" prop="name">
              <el-input v-model="addForm.name" />
            </el-form-item>
            <el-form-item label="登录账号" prop="userName">
              <el-input v-model="addForm.userName" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phonenumber">
              <el-input v-model="addForm.phonenumber" />
            </el-form-item>
            <el-form-item label="分配角色" prop="roleId">
              <el-select v-model="addForm.roleId" placeholder="请选择角色">
                <el-option
                  v-for="item in roleList"
                  :key="item.roleId"
                  :label="item.roleName"
                  :value="item.roleId"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="员工状态" prop="status">
              <el-radio-group v-model="addForm.status">
                <el-radio :label="0">禁用</el-radio>
                <el-radio :label="1">启用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </div>
        <template #footer>
          <el-button size="mini" @click="closeDialog">取 消</el-button>
          <el-button size="mini" type="primary" @click="submit">确 定</el-button>
        </template>
      </el-dialog>
      <el-dialog
        :visible.sync="Visible"
        :show-close="false"
        width="400px"
      >
        <div>
          <span><i class="el-icon-warning" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;确定将密码重置为123456?</span>
        </div>
        <span slot="footer">
          <el-button @click="DialogTwo">取 消</el-button>
          <el-button type="primary" @click="delSubmitTwo">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { userList, delList, getRole, addList, editList, editSubmitList, resetPassword } from '@/api/parkUser'
export default {
  name: 'Employee',
  data() {
    return {
      idTwo: '',
      Visible: false,
      type: '',
      roleList: [],
      name: '',
      page: 1,
      pageSize: 10,
      tableData: [],
      total: '',
      dialogVisible: false,
      // 表单校验
      addForm: {
        id: '',
        name: '',
        userName: '',
        phonenumber: '',
        roleId: '',
        status: 0
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入名字', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请分配角色', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title() {
      if (this.type === 'add') {
        return '添加'
      } else {
        return '编辑'
      }
    }
  },
  created() {
    this.List()
    this.Role()
  },
  methods: {
    async delSubmitTwo() {
      this.Visible = false
      // 点击确定重置
      await resetPassword(this.idTwo)
      this.List()
    },
    // 点击重置第二个dialog打开
    reset(id) {
      this.idTwo = id
      this.Visible = true
    },
    // 点击取消第一个dialog
    DialogTwo() {
      this.Visible = false
    },
    // 切换每页条数设置设置
    handleSizeChange(val) {
      this.pageSize = val
      this.List()
    },
    async Role() {
      const res = await getRole()
      // console.log(res)
      this.roleList = res.data
    },
    // 获取列表
    async List() {
      const res = await userList({
        name: this.name,
        page: this.page,
        pageSize: this.pageSize
      })
      // console.log(res)
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    // 分页
    handleCurrentChange(val) {
      this.page = val
      this.List()
    },
    // 点击关闭
    closeDialog() {
      this.dialogVisible = false
      this.$refs.addForm.resetFields()
    },
    // 点击添加
    add(type) {
      this.type = type
      this.dialogVisible = true
    },
    // 点击编辑
    async edit(type, id) {
      this.type = type
      this.dialogVisible = true
      const res = await editList(id)
      this.addForm = res.data
    },
    // 点击删除
    async del(id) {
      this.$confirm('删除员工后将不可登录，确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delList(id)
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        this.List()
      })
    },
    // 点击确定按钮
    submit() {
      this.$refs.addForm.validate(async(valid) => {
        if (!valid) return
        if (this.type === 'add') {
          await addList(this.addForm)
          this.$message.success('添加成功')
        } else {
          await editSubmitList(this.addForm)
          this.$message.success('编辑成功')
        }
        this.dialogVisible = false
        this.$refs.addForm.resetFields()
        this.List()
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.div {
  padding-top: 10px;
  border-top: 1.5px solid #f4f6f8;
}

.block {
  text-align: right;
}
.el-icon-warning{
  color:#ffb418 ;
}
</style>
